package com.haiyu.manager.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.haiyu.manager.dao.Category1Mapper;
import com.haiyu.manager.dao.Category2Mapper;
import com.haiyu.manager.dto.Category1DTO;
import com.haiyu.manager.pojo.Category1;
import com.haiyu.manager.pojo.Category2;
import com.haiyu.manager.response.PageDataResult;
import com.haiyu.manager.service.Category1Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class Category1ServiceImpl implements Category1Service {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private Category1Mapper category1Mapper;
    @Autowired
    private Category2Mapper category2Mapper;


    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    @Override
    public PageDataResult geCategory1List(Integer pageNum, Integer pageSize) {
        PageDataResult pageDataResult = new PageDataResult();
        List<Category1DTO> category1s = category1Mapper.getCategory1List();

        PageHelper.startPage(pageNum, pageSize);

        if(category1s.size() != 0){
            PageInfo<Category1DTO> pageInfo = new PageInfo<>(category1s);
            pageDataResult.setList(category1s);
            pageDataResult.setTotals((int) pageInfo.getTotal());
        }
        return pageDataResult;

    }

    @Override
    public List<Category1DTO> getCategory1s() {
        return category1Mapper.getCategory1List();
    }

    @Override
    public Category1 findCateory1ById(Integer category1Id) {
        return category1Mapper.selectByPrimaryKey(category1Id);
    }

    @Override
    public Map<String, Object> updateCategory1(Category1 category1) {
        Map<String,Object> data = new HashMap();
        try{

            int result = category1Mapper.updateCategory1(category1);
            if(result == 0){
                data.put("code",0);
                data.put("msg","更新失败！");
                logger.error("category1[更新]，结果=更新失败！");
                return data;
            }
            data.put("code",1);
            data.put("msg","更新成功！");
            logger.info("category1[更新]，结果=更新成功！");
        }catch (Exception e) {
            e.printStackTrace();
            logger.error("category1[更新]异常！", e);
            return data;
        }
        return data;
    }

    @Override
    public Map<String, Object> delCategory1(Integer category1Id, Integer status) {
        Map<String, Object> data = new HashMap<>();
        try {
            if(category2Mapper.getCategory2By1Id(category1Id).size()!=0){
                data.put("code", 0);
                data.put("msg", "删除category1失败,其下可能存在二级分类服务");
            }else {
                int result = category1Mapper.updateCategory1Status(category1Id, status);
                if (result == 0) {
                    data.put("code", 0);
                    data.put("msg", "删除category1失败");
                }
                data.put("code", 1);
                data.put("msg", "删除category1成功");
            }

        } catch (Exception e) {
            e.printStackTrace();
            logger.error("删除category1异常！", e);
        }
        return data;
    }

    @Override
    public Map<String, Object> recoverCategory1(Integer category1Id, Integer status) {
        Map<String, Object> data = new HashMap<>();
        try {
            int result =category1Mapper.updateCategory1Status(category1Id,status);
            if(result == 0){
                data.put("code",0);
                data.put("msg","恢复category1失败");
            }
            data.put("code",1);
            data.put("msg","恢复category1成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("恢复category1异常！", e);
        }
        return data;
    }

    @Override
    public Map<String, Object> addCategory1(Category1 category1) {
        Map<String,Object> data = new HashMap();
        try {
            //////
            Category1 old = category1Mapper.getCategory1ByName(category1.getCategory1Name(),null);
            if(old != null){
                data.put("code",0);
                data.put("msg","类别名已存在！");
                logger.error("类别名[新增]，结果=类别名已存在！");
                return data;
            }

            /////////
            int result = category1Mapper.insert(category1);
            System.out.println(result);
            if(result == 0){
                data.put("code",0);
                data.put("msg","新增category1失败");
                logger.error("新增category1失败");
                return data;
            }
            data.put("code",1);
            data.put("msg","新增category1成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("添加category1并授权！异常！", e);
        }
        return data;
    }

}
